package communication.thirdLayer;


import communication.thirdLayer.*;
import communication.pack.*;
import kernel.*;


public class TimeOut extends Thread {

    private Simulator _s;

    private double _start;

    private Integer _id;

    private boolean _over;
    
    private static final int TIMEOUT = 7000;

    public TimeOut(Simulator s, Integer x, double start){
	super();
	_s=s;
	_id=x;
	_start=start;
	_over = false;
    }

    public void setStart(double x) {
	_start=x;
    }

    public void setOver(){
	_over = true;
    }
    
    public void run() {
	while(_s.getTime() < _start + (_s.getCommunicationManager().getTLManager().getTLProtocol().getNetwork().size())*TIMEOUT && !_over);
	if(!_over) {
	    SensorRouting sr = (SensorRouting)_s.getCommunicationManager().getTLManager().getTLProtocol();
	    if(sr instanceof DirectedDiffusion){
		if(((DirectedDiffusion)sr).getResults().get(_id).getState().equals(DirectedResults.ROUTING)){
		    ((DirectedDiffusion)sr).getResults().get(_id).setState(DirectedResults.FAILED);
		}
	    }
	}
    }
    
}
